﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.PowerPoint.STAMP.Core;

namespace Microsoft.Office.PowerPoint.STAMP
{
    /// <summary>
    /// Converts the various types of alignment to each other.
    /// </summary>
    public static class AlignmentConverter
    {
        /// <summary>
        /// Converts <see cref="HorizontalAlignment"/> to <see cref="TextAlignAttibuteValue"/>
        /// </summary>
        /// <param name="alignment">The alignment.</param>
        /// <returns></returns>
        public static TextAlignAttibuteValue ConvertToTextAlignStyle(HorizontalAlignment alignment)
        {
            switch (alignment)
            {
                case HorizontalAlignment.Center:
                    return TextAlignAttibuteValue.center;
                case HorizontalAlignment.Right:
                    return TextAlignAttibuteValue.right;
                default:
                    return TextAlignAttibuteValue.left;
            }
        }

        /// <summary>
        /// Converts <see cref="HorizontalAlignment"/> to <see cref="DataGridViewContentAlignment"/>
        /// </summary>
        /// <param name="alignment">The alignment.</param>
        /// <returns></returns>
        public static DataGridViewContentAlignment ConvertToDatagridAlignment(HorizontalAlignment alignment)
        {
            switch (alignment)
            {
                case HorizontalAlignment.Center:
                    return DataGridViewContentAlignment.MiddleCenter;
                case HorizontalAlignment.Right:
                    return DataGridViewContentAlignment.MiddleRight;
                default:
                    return DataGridViewContentAlignment.MiddleLeft;
            }
        }

        /// <summary>
        /// Converts <see cref="TextAlignAttibuteValue"/> to <see cref="HorizontalAlignment"/>, returning null if no valid value exists.
        /// </summary>
        public static HorizontalAlignment? ConvertToHorizontalAlignment(TextAlignAttibuteValue textAlignment)
        {
            switch (textAlignment)
            {
                case TextAlignAttibuteValue.center:
                    return HorizontalAlignment.Center;
                case TextAlignAttibuteValue.left:
                    return HorizontalAlignment.Left;
                case TextAlignAttibuteValue.right:
                    return HorizontalAlignment.Right;
                default:
                    return null;
            }
        }

        /// <summary>
        /// Converts to paragraph alignment.
        /// </summary>
        /// <param name="captionStyle">The caption style.</param>
        /// <returns></returns>
        public static PpParagraphAlignment ConvertToParagraphAlignment(CaptionStyle captionStyle)
        {
            switch (captionStyle.TextAlignStyleProperty)
            {
                case TextAlignAttibuteValue.center:
                    return PpParagraphAlignment.ppAlignCenter;
                case TextAlignAttibuteValue.right:
                case TextAlignAttibuteValue.end:
                    return PpParagraphAlignment.ppAlignRight;
                default: 
                    return PpParagraphAlignment.ppAlignLeft;
            }
        }

        /// <summary>
        /// Converts to content alignment.
        /// </summary>
        public static ContentAlignment ConvertToContentAlignment(HorizontalAlignment textAlignment)
        {
            switch (textAlignment)
            {
                case HorizontalAlignment.Center:
                    return ContentAlignment.MiddleCenter;
                case HorizontalAlignment.Right:
                    return ContentAlignment.MiddleRight;
                default:
                    return ContentAlignment.MiddleLeft;
            }
        }

        /// <summary>
        /// Converts to string alignment.
        /// </summary>
        /// <param name="alignment">The alignment.</param>
        /// <returns></returns>
        public static StringAlignment ConvertToStringAlignment(HorizontalAlignment? alignment)
        {
            switch (alignment)
            {
                case HorizontalAlignment.Center:
                    return StringAlignment.Center;
                case HorizontalAlignment.Right:
                    return StringAlignment.Far;
                default:
                    return StringAlignment.Near;
            }
        }
    }
}